package com.decode;

import android.util.Log;

import com.DataProcess.CanDataType;
import com.pudu.mydemo.nativeClass.NativeClass;

import java.util.Arrays;

/**
 * Created by Administrator on 2016/7/30 0030.
 */
public class Decode {
    public short[] speed = {0,0};

    public int uwbID = 0;
    public short uwb = 10000;
    public int uwbID2 = 0;
    public short uwbDistance2 = 0;
    public short[] ird = {0,0,0,0, 0,0,0,0};
    public short[] drop = {0,0};
    public short[] plate = {0,0};
    public short batterySurplus = 0;
    public short batteryVoltage = 0;
    public short [] imu = {0,0,0};
    public boolean chargeStatus = false;
    public byte[] data ={0,0,0,0, 0,0,0,0};


    public void byteArray2num(byte [] data) {
        if(data != null){
            this.data = data;
            Log.i("decode","data to decode:"+ Arrays.toString(data));
            switch (data[0]) {
                case 0x00:
                    break;
                case 0x01:
                    speedsDecode(data);
                    break;
                case 0x02:
                    break;
                case 0x03:
                    uwbsDecode(data);
                    break;
                case 0x04:
                    irdDecoede(data);
                    break;
                case 0x05:
                    irdDecoede(data);
                    break;
                case 0x06:
                    dropDecode(data);
                    break;
                case 0x07:
                    plateDecode(data);
                    break;
                case 0x09:
                    batteryDecode(data);
                    break;
                case 0x0b:
                    imuDecode(data);
                    break;
                case 0x0d:
                    chargeDecode(data);
                    break;
                default:
                    Log.i("decode","not to decode:"+ Arrays.toString(data));
            }
        }
    }

    void speedsDecode(byte[] data) {
        speed[0] = (short)(( (data[1]&0x00ff) << 8) |  (data[2]&0x00ff) );
        speed[1] = (short)(( (data[3]&0x00ff) << 8) |  (data[4]&0x00ff) );
        Log.i("decode","data:"+ Arrays.toString(data)+"speed:"+Arrays.toString(speed));
    }


    public void uwbsDecode(byte[] data) {
//        uwbID = (short) (((data[1] & 0x00ff) << 8) | (data[2] & 0x00ff));
//        uwb = (short) (((data[3] & 0x00ff) << 8) | (data[4] & 0x00ff));
//        Log.i("decode","data:"+ Arrays.toString(data)+"uwb"+uwbID+":"+uwb);
        uwbID = (short) ((data[1] & 0x00ff));
        uwb = (short) (((data[2] & 0x00ff) << 8) | (data[3] & 0x00ff));
        uwbID2 = (short) ((data[4] & 0x00ff));
        uwbDistance2 = (short) (((data[5] & 0x00ff) << 8) | (data[6] & 0x00ff));
    }

    public void irdDecoede(byte[] data){
        int i;
        if (data[0] == 0x04) {
             i = 0;
        } else {
             i = 4;
        }
        for(int j=0;j<4;j++) {
            //ird[i] = data[j+1];
            ird[i] = (short)(data[j+1] & 0x00ff);
            i++;
        }
        Log.i("decode","data:"+ Arrays.toString(data)+"ird:"+Arrays.toString(ird));
    }

    public void dropDecode(byte[] data) {
        drop[0] = (short)(data[1] & 0x00ff);
        drop[1] = (short)(data[2] & 0x00ff);
        Log.i("decode","data:"+ Arrays.toString(data)+"drop:"+Arrays.toString(drop));
    }

    public void plateDecode(byte[] data) {
        plate[0] = (short) (((data[1] & 0x00ff) << 8) | (data[2] & 0x00ff));
        plate[1] = (short) (((data[3] & 0x00ff) << 8) | (data[4] & 0x00ff));
        Log.i("decode","data:"+ Arrays.toString(data)+"plate:"+Arrays.toString(plate));
    }

    public void batteryDecode(byte[] data) {
        batterySurplus = (short)(data[1] & 0x00ff);
        batteryVoltage = (short)(data[2] & 0x00ff);
        Log.i("decode","data:"+ Arrays.toString(data)+"batterSurplus:"+batterySurplus+"  batterVoltage:"+batteryVoltage);
    }

    public void imuDecode(byte[] data) {
        imu[0] = (short) (((data[1] & 0x00ff) << 8) | (data[2] & 0x00ff));
        imu[1] = (short) (((data[3] & 0x00ff) << 8) | (data[4] & 0x00ff));
        imu[2] = (short) (((data[5] & 0x00ff) << 8) | (data[6] & 0x00ff));
        Log.i("decode","data:"+ Arrays.toString(data)+"imu:"+Arrays.toString(imu));
    }

    public void chargeDecode(byte[] data) {
        if(data[1] > 0x00) chargeStatus = true;
        else chargeStatus = false;
        Log.i("decode","data:"+ Arrays.toString(data)+"chargeStatus:"+chargeStatus);
    }
}
